//
//  QTInfoDetailViewController.m
//  qdqts
//
//  Created by jonn on 14-6-23.
//  Copyright (c) 2014年 seas.cheng. All rights reserved.
//

#import "QTInfoDetailViewController.h"
#import "QTClient.h"

@interface QTInfoDetailViewController ()

@end

@implementation QTInfoDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title=@"质检详情";
    // Do any additional setup after loading the view from its nib.

    NSLog(@"%@",_puid);
    
    [_activity startAnimating];
    [_activity setHidesWhenStopped:YES];
    [_failure setHidden:YES];
    [_infoWebView setHidden:YES];
    
    [[QSClient instance] loadNewsDetail:_puid completionBlock:^(ArticleDetail *articleDetail, NSError *error){
        if (error) {
//            self.failure.text=@"无法连接服务器，请检查网络";
//            [self.failure setHidden:NO];
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示"
                                                          message:[NSString stringWithFormat:@"无法连接服务器，请稍后在试",nil]
                                                         delegate:self
                                                cancelButtonTitle:@"确定"
                                                otherButtonTitles:nil, nil];
            [alert show];
        }
        else if (articleDetail == nil) {
            //[self showAlert:@"加载数据失败！！"];
            [_activity stopAnimating];
            [_failure setHidden:NO];
        }
        else{
            _failure.hidden=YES;
            [_activity setHidden:YES];
            [_infoWebView setHidden:NO];
            
            _newsDetail = articleDetail;
            NSString *yearString = [_newsDetail.createTime substringToIndex:4];
            NSRange monthRange = NSMakeRange(4, 2);
            NSString *monthString = [_newsDetail.createTime substringWithRange:monthRange];
            NSRange dayRange = NSMakeRange(6, 2);
            NSString *dayString = [_newsDetail.createTime substringWithRange:dayRange];
            
            
            NSString *title=[NSString stringWithFormat:@"<p style=\"padding: 0px; margin-top: 10px; margin-bottom: 10px; font-size: 17px; color: rgb(37, 37, 37); font-family: verdana,arial,tahoma; text-align: center; font-weight: bold;\">%@</p><p style=\"padding-top: 4px; margin-top: 10px; font-size: 10px; color: rgb(37, 37, 37); font-family: verdana,arial,tahoma; text-align: center; font-weight: bold;\">来源:%@&nbsp;&nbsp;&nbsp作者：%@&nbsp;&nbsp;&nbsp时间：%@&nbsp;&nbsp;&nbsp浏览数：%@</p><HR>",_newsDetail.articleTitle,_newsDetail.resourceFrom,_newsDetail.author,[NSString stringWithFormat:@"%@-%@-%@",yearString, monthString, dayString],_newsDetail.viewCount,nil];
            
            NSString *htmlString=[NSString stringWithFormat:@"<html> \n"
                                  "<head> \n"
                                  "<style type=\"text/css\"> \n"
                                  "body { font-family: Helvetica !important; font-size:1em !important;  color: #000000 !important;  background-color:#FFFFFF !important;}\n"
                                  "</style> \n"
                                  "</head> \n"
                                  "%@"
                                  "<body>%@</body> \n"
                                  "</html>", title, _newsDetail.articleContent.content];
            [_infoWebView loadHTMLString:htmlString baseURL:nil];
            // [webView setScalesPageToFit:YES];
        }
    }];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
